package com.block.channel.flow.channel;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.channel.flow.IChannelReportService;
import com.block.channel.model.result.ReprotFlowResult;
import com.block.common.bean.CommonParam;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelBaseInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SuoZhiChannelReportService implements IChannelReportService {

    private static Logger log = LogFactory.getCommLog();

    private String requestUrl = "https://api.suozhikeji.cn/api/reportEvent";
    private String appkey = "ad_key_100909";
    private String code = "ad_code_90001888";

    @Override
    public ReprotFlowResult registerActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        ReprotFlowResult flowResult = new ReprotFlowResult();
        flowResult.setSuccess(false);

        JSONObject headObject = commonParam.getHeadObject();
        String sz_rli = headObject.getString("sz_rli");
        if (StrUtil.isEmpty(sz_rli)){
            flowResult.setCode("-2");
            flowResult.setMessage("sz_li参数为空，不做处理");
            return flowResult;
        }

        String time = String.valueOf(System.currentTimeMillis());
        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("appkey", appkey);
        bizMap.put("logId", sz_rli);
        bizMap.put("time", time);
        bizMap.put("eventType", "register");

        String sign = this.getSign(bizMap);
        bizMap.put("sign", sign);

        log.info("【索知信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【索知信息流上报】【注册】【开始结束】【result】{}", response);

        JSONObject jsonObject = JSONObject.parseObject(response);
        Integer code = jsonObject.getInteger("code");
        String message = jsonObject.getString("message");

        flowResult.setCode(String.valueOf(code));
        flowResult.setMessage(message);
        flowResult.setParamInfo(JSON.toJSONString(bizMap));
        flowResult.setResultInfo(response);
        if (StrUtil.equals(String.valueOf(code), "200")){
            flowResult.setSuccess(true);
        }else{
            flowResult.setSuccess(false);
        }
        return flowResult;
    }

    @Override
    public ReprotFlowResult downloadActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }

    @Override
    public ReprotFlowResult payBuyActionReport(CommonParam commonParam, ChannelBaseInfo channelBaseInfo) {
        return null;
    }

    public String getSign(Map<String, Object> bizMap){

        List<String> keyList = JSON.parseArray(JSON.toJSONString(bizMap.keySet()), String.class);
        Collections.sort(keyList);
        StringBuilder stringBuilder = new StringBuilder();
        for (String key: keyList){
            stringBuilder.append(StrUtil.format("{}={}", key, bizMap.get(key))).append("&");
        }
        stringBuilder.append(StrUtil.format("code={}", code));
        System.out.println(stringBuilder.toString());
        return SecureUtil.md5(stringBuilder.toString()).toLowerCase();

    }
}
